﻿using LightingWebService;
using Server.Resource;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml.Linq;

namespace AuroraPlatform.Web
{
    /// <summary>
    /// Summary description for WebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService
    {
        static WebService()
        {
            try
            {
                string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ServerConfig.xml");
                XDocument doc = XDocument.Load(configPath);
                XElement root = doc.Root;

                ServerConfig.Singleton.Database = root.Element("Database").Value;
                ServerConfig.Singleton.TemplateRoot = root.Element("TemplateRoot").Value;
                ServerConfig.Singleton.ImgRoot = root.Element("ImgRoot").Value;
                ServerConfig.Singleton.ExportRoot = root.Element("ExportRoot").Value;
                ServerConfig.Singleton.ExportUrl = root.Element("ExportUrl").Value;
                ServerConfig.Singleton.Debug = root.Element("Debug").Value == "1";
            }
            catch (Exception ex)
            {
                LightingFacade lf = new LightingFacade();
                lf.Log("读取配置清单", ex.StackTrace);
            }
        }

        [WebMethod]
        public bool SignLable(string name)
        {
            LightingFacade os = new LightingFacade();
            return os.SignLable(name);
        }

        [WebMethod]
        public bool UnSignLable(string name)
        {
            LightingFacade os = new LightingFacade();
            return os.UnSignLable(name);
        }

        [WebMethod]
        public byte[] GetStaticResource()
        {
            byte[] result = null;
            LightingFacade service = new LightingFacade();
            try
            {
                result = service.GetStaticResource();
            }
            catch (Exception ex)
            {
                service.Log("获取静态资源", ex.StackTrace);
                result = new byte[0];
            }

            return result;
        }

        [WebMethod]
        public byte[] SearchProdName()
        {
            byte[] result = null;
            LightingFacade service = new LightingFacade();
            try
            {
                result = service.SearchProdName();
            }
            catch (Exception ex)
            {
                service.Log("获取产品品牌", ex.StackTrace);
                result = new byte[0];
            }

            return result;
        }

        [WebMethod]
        public byte[] Login(string name, string pwd)
        {
            byte[] result = null;
            LightingFacade service = new LightingFacade();
            try
            {
                result = service.Login(name, pwd);
            }
            catch (Exception ex)
            {
                service.Log("用户登陆", ex.StackTrace);
                result = new byte[0];
            }

            return result;
        }

        [WebMethod]
        public byte[] SearchProdList(byte[] conditions)
        {
            byte[] result = null;
            LightingFacade service = new LightingFacade();
            try
            {
                result = service.SearchProdList(conditions);
            }
            catch (Exception ex)
            {
                service.Log("主界面搜索", ex.StackTrace);
                result = new byte[0];
            }

            return result;
        }

        [WebMethod]
        public byte[] GetUsers()
        {
            byte[] result = null;
            LightingFacade service = new LightingFacade();
            try
            {
                result = service.GetUsers();
            }
            catch (Exception ex)
            {
                service.Log("获取所有用户", ex.StackTrace);
                result = new byte[0];
            }

            return result;
        }

        [WebMethod]
        public string AddUser(byte[] bytes)
        {
            string result = null;
            LightingFacade service = new LightingFacade();
            try
            {
                result = service.AddUser(bytes);
            }
            catch (Exception ex)
            {
                service.Log("添加用户", ex.StackTrace);
            }

            return result;
        }

        [WebMethod]
        public bool UpdateUser(byte[] bytes)
        {
            bool result = false;
            LightingFacade service = new LightingFacade();
            try
            {
                result = service.UpdateUser(bytes);
            }
            catch (Exception ex)
            {
                service.Log("修改用户", ex.StackTrace);
            }

            return result;
        }

        [WebMethod]
        public bool DeleteUser(string id)
        {
            bool result = false;
            LightingFacade service = new LightingFacade();
            try
            {
                result = service.DeleteUser(id);
            }
            catch (Exception ex)
            {
                service.Log("删除用户", ex.StackTrace);
            }

            return result;
        }

        [WebMethod]
        public string AddProd(byte[] bytes)
        {
            string result = null;
            LightingFacade service = new LightingFacade();
            try
            {
                result = service.AddProd(bytes);
            }
            catch (Exception ex)
            {
                service.Log("添加灯具", ex.StackTrace);
            }

            return result;
        }

        [WebMethod]
        public bool UpdateProd(byte[] bytes)
        {
            bool result = false;
            LightingFacade service = new LightingFacade();
            try
            {
                result = service.UpdateProd(bytes);
            }
            catch (Exception ex)
            {
                service.Log("修改灯具", ex.StackTrace);
            }

            return result;
        }

        [WebMethod]
        public bool DeleteProd(string id)
        {
            bool result = false;
            LightingFacade service = new LightingFacade();
            try
            {
                result = service.DeleteProd(id);
            }
            catch (Exception ex)
            {
                service.Log("删除灯具", ex.StackTrace);
            }

            return result;
        }

        [WebMethod]
        public byte[] SearchProdClass(byte[] conditions)
        {
            byte[] result = null;
            LightingFacade service = new LightingFacade();
            try
            {
                result = service.SearchProdClass(conditions);
            }
            catch (Exception ex)
            {
                service.Log("获取灯具分类", ex.StackTrace);
                result = new byte[0];
            }

            return result;
        }

        [WebMethod]
        public byte[] SearchProdSeries(byte[] conditions)
        {
            byte[] result = null;
            LightingFacade service = new LightingFacade();
            try
            {
                result = service.SearchProdSeries(conditions);
            }
            catch (Exception ex)
            {
                service.Log("获取灯具系列", ex.StackTrace);
                result = new byte[0];
            }

            return result;
        }

        [WebMethod]
        public byte[] SearchPrimaryProdInfo(string id)
        {
            byte[] result = null;
            LightingFacade service = new LightingFacade();
            try
            {
                result = service.SearchPrimaryProdInfo(id);
            }
            catch (Exception ex)
            {
                service.Log("获取灯具主要信息", ex.StackTrace);
                result = new byte[0];
            }

            return result;
        }

        [WebMethod]
        public string SearchPrimaryProdInfoToPDF(string id)
        {
            string result = null;
            LightingFacade service = new LightingFacade();
            try
            {
                result = service.SearchPrimaryProdInfoToPDF(id);
            }
            catch (Exception ex)
            {
                service.Log("获取灯具主要信息导出PDF", ex.StackTrace);
                result = string.Empty;
            }

            return result;
        }

        [WebMethod]
        public string SearchPrimaryProdInfoToExcel(string id)
        {
            string result = null;
            LightingFacade service = new LightingFacade();
            try
            {
                result = service.SearchPrimaryProdInfoToExcel(id);
            }
            catch (Exception ex)
            {
                service.Log("获取灯具主要信息导出Excel", ex.StackTrace);
                result = string.Empty;
            }

            return result;
        }

        [WebMethod]
        public byte[] SearchProdInfo(string id)
        {
            byte[] result = null;
            LightingFacade service = new LightingFacade();
            try
            {
                result = service.SearchProdInfo(id);
            }
            catch (Exception ex)
            {
                service.Log("获取灯具全部信息", ex.StackTrace);
                result = new byte[0];
            }

            return result;
        }

        [WebMethod]
        public string SearchProdInfoToPDF(string id)
        {
            string result = null;
            LightingFacade service = new LightingFacade();
            try
            {
                result = service.SearchProdInfoToPDF(id);
            }
            catch (Exception ex)
            {
                service.Log("获取灯具全部信息导出PDF", ex.StackTrace);
                result = string.Empty;
            }

            return result;
        }

        [WebMethod]
        public string SearchProdInfoToExcel(string id)
        {
            string result = null;
            LightingFacade service = new LightingFacade();
            try
            {
                result = service.SearchProdInfoToExcel(id);
            }
            catch (Exception ex)
            {
                service.Log("获取灯具全部信息导出Excel", ex.StackTrace);
                result = string.Empty;
            }

            return result;
        }

        [WebMethod]
        public byte[] SearchRelations(string id)
        {
            LightingFacade service = new LightingFacade();
            return service.SearchRelations(id);
        }

        [WebMethod]
        public bool BatchDeleteProd(byte[] bytes)
        {
            LightingFacade service = new LightingFacade();
            return service.BatchDeleteProd(bytes);
        }

        [WebMethod]
        public string CustomExportToExcel(byte[] bytes)
        {
            LightingFacade service = new LightingFacade();
            return service.CustomExportToExcel(bytes);
        }

        [WebMethod]
        public string CustomExportToPDF(byte[] bytes)
        {
            LightingFacade service = new LightingFacade();
            return service.CustomExportToPDF(bytes);
        }

        [WebMethod]
        public byte[] SearchNewProds()
        {
            LightingFacade service = new LightingFacade();
            return service.SearchNewProds();
        }

        [WebMethod]
        public string SearchProdListToPDF(byte[] bytes)
        {
            LightingFacade service = new LightingFacade();
            return service.SearchProdListToPDF(bytes);
        }

        [WebMethod]
        public string SearchProdListToExcel(byte[] bytes)
        {
            LightingFacade service = new LightingFacade();
            return service.SearchProdListToExcel(bytes);
        }

        [WebMethod]
        public string SearchProdSeriesToPDF(byte[] bytes)
        {
            LightingFacade service = new LightingFacade();
            return service.SearchProdSeriesToPDF(bytes);
        }

        [WebMethod]
        public string SearchProdSeriesToExcel(byte[] bytes)
        {
            LightingFacade service = new LightingFacade();
            return service.SearchProdSeriesToPDF(bytes);
        }

         [WebMethod]
        public string SearchNewProdsToPDF()
        {
            LightingFacade service = new LightingFacade();
            return service.SearchNewProdsToPDF();
        }

         [WebMethod]
        public string SearchNewProdsToExcel()
        {
            LightingFacade service = new LightingFacade();
            return service.SearchNewProdsToExcel();
        }
    }
}
